package com.wlc.io;

import java.io.*;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description IO 的 practice
 * @date 2021-07-11 10:19
 */
public class IoPractice {

    private static final String SOURCE_SRC = "/Users/wanglichao/Desktop/temp/test.txt";
    private static final String TARGET_SRC = "/Users/wanglichao/Desktop/temp/test6.txt";

    /**
     * 复制文件
     *
     * @param sourceSrc 源文件
     * @param targetSrc 目标文件
     */
    public void copyFile(String sourceSrc, String targetSrc) {

        File fileSource = new File(SOURCE_SRC);
        File fileTarget = new File(TARGET_SRC);
        try (FileReader fileReader = new FileReader(fileSource);
             BufferedReader bufferedReader = new BufferedReader(fileReader);
             FileWriter fileWriter = new FileWriter(fileTarget);
             PrintWriter printWriter = new PrintWriter(fileWriter)) {
            // 1 、读取文件
            String line = null;
            StringBuffer sb = new StringBuffer();
            while (true) {
                line = bufferedReader.readLine();
                if (null == line) {
                    break;
                }
                sb.append(line);
            }
            System.out.println("读取到的内容为：" + sb.toString());
            System.out.println("开始写入数据" + sb.toString());
            // 2、写入数据
            printWriter.write(sb + "\n");
            printWriter.flush();
            System.out.println("数据写入完毕。。。");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * @param file   查找的目录
     * @param search 查找的字符串
     */
    public static void search(File file, String search) {
        if (file.isFile()) {
            if (file.getName().toLowerCase().endsWith(".java")) {
                String fileContent = readFileConent(file);
                if (fileContent.contains(search)) {
                    System.out.printf("找到子目标字符串%s,在文件:%s%n", search, file);
                }
            }
        }
        if (file.isDirectory()) {
            File[] fs = file.listFiles();
            for (File f : fs) {
                search(f, search);
            }
        }
    }

    public static String readFileConent(File file) {
        try (FileReader fr = new FileReader(file)) {
            char[] all = new char[(int) file.length()];
            fr.read(all);
            return new String(all);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String readFile(File file) {
        try (FileReader fileReader = new FileReader(file)) {
            char[] chars = new char[(int) file.length()];
            fileReader.read(chars);
            return new String(chars);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 读取文件夹，查找目录下是否有要找的文件，并且包含要查找的关键字
     * @param file
     * @param search
     */
    public static void read(File file, String search) {


        if (file.isFile()) {
            if (file.getName().toLowerCase().endsWith(".java")) {
                System.out.println("存在");
                // 读取文件
                String readFile = readFile(file);
                if (readFile.contains(search)) {
                    System.out.println("找到了关键字：" + search + " 在" + file.getPath() + "  中");
                }else{
                    System.out.println("没有找到关键字 " + search);
                }
            } else {
                //System.out.println("不存在");

            }
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                read(files[i], search);
            }

        }

    }


    public static void main(String[] args) {
        IoPractice ioPractice = new IoPractice();
        File file = new File("/Users/wanglichao/Desktop/temp/");
        //search(file,"User");
        read(file, "User");
    }
}
